package com.etc.traval.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.traval.biz.StrategyBiz;
import com.etc.traval.biz.StrategycomBiz;
import com.etc.traval.bizimpl.StrategyBizImpl;
import com.etc.traval.bizimpl.StrategycomBizImpl;
import com.etc.traval.util.MyUtil;
import com.etc.travel.entity.Strategy;
import com.etc.travel.entity.Strategycom;
import com.etc.travel.entity.User;
import com.google.gson.Gson;

public class StrategyServlet extends HttpServlet {

	/**
		 * Constructor of the object.
		 */
	public StrategyServlet() {
		super();
	}

	/**
		 * Destruction of the servlet. <br>
		 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
		 * The doGet method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to get.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
		
	}

	/**
		 * The doPost method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to post.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String flag=request.getParameter("flag");
		StrategyBiz sbiz=new StrategyBizImpl();
		if(flag.equals("add")){
			String title=request.getParameter("title");
			String content=request.getParameter("content");
			User user=(User)request.getSession().getAttribute("user");
			Strategy strategy=new Strategy();
			strategy.setStrategy_user(user);
			strategy.setStrategy_content(content);
			strategy.setStrategy_title(title);
			strategy.setStrategy_time(MyUtil.getWebsiteDatetime());
			strategy.setStrategy_state("未通过");
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out=response.getWriter();
			if(sbiz.strategyAdd(strategy)){
				out.print("<script>alert('发表成功');location.href='guides.jsp'</script>");
			}else{out.print("<script>alert('发表失败');location.href='guides.jsp'</script>");}
			out.flush();
			out.close();
		}else if("query".equals(flag)){
			List<Strategy> list=sbiz.queryAllStrategy(Integer.parseInt(request.getParameter("page")), Integer.parseInt(request.getParameter("size")));
			//封装json
			Gson gson=new Gson();
			String gsonString=gson.toJson(list);
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
			//输出
			out.print(gsonString);
			out.flush();
			out.close(); 		
		}else if("info".equals(flag)){
			int id=Integer.parseInt(request.getParameter("id"));
			Strategy strategy=sbiz.querySTrategyById(id);
			request.setAttribute("info", strategy);
			StrategycomBiz scdao=new StrategycomBizImpl();
			int counts=scdao.qurtyallCount(id);
			request.setAttribute("counts", counts);
			if(counts>0){
			List<Strategycom> scom=scdao.queryallStrategycom(id, 1, 5);
			request.setAttribute("scom", scom);			
			}
			request.getRequestDispatcher("thestrategy.jsp").forward(request, response);
		}
		
	}

	/**
		 * Initialization of the servlet. <br>
		 *
		 * @throws ServletException if an error occurs
		 */
	public void init() throws ServletException {
		// Put your code here
	}

}
